﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Comet.Core;
using Pixysoft.Framework.Remoting;
using Pixysoft.Framework.Verifications;
using Pixysoft.Web;

namespace Pixysoft.Framework.Comet
{
    public class CometManager
    {
        public static ICometMessage CreateMessage(string senderCode, string receiverCode)
        {
            if (!Verification.Verifies(senderCode, receiverCode).IsNotNullOrEmpty().Result())
                throw Exceptions.ManualVerificationFailedException("missing parameters");

            senderCode = senderCode.Trim().ToUpper();
            receiverCode = receiverCode.Trim().ToUpper();

            CometMessage message = new CometMessage();
            message.CreateDate = Pixysoft.Tools.GlobalTimer.Instance.GetGlobalTime();
            message.MessageCode = StringHelper.GetMessageCode();
            message.ModiDate = Pixysoft.Tools.GlobalTimer.Instance.GetGlobalTime();
            message.ReceiverCode = receiverCode;
            message.SenderCode = senderCode;
            //message.TimeStamp = StringHelper.GetTimestamp();//这个由服务器控制 不能由客户端控制
            return message;
        }

        public static ICometMessageHelper CometMessageHelper
        {
            get
            {
                return new CometMessageHelper();
            }
        }


        public static IRemoteChannel<ICometRemoting> CreateRemoting(string url)
        {
            if (!Verification.Verifies(url).IsNotNullOrEmpty().Result())
                throw Exceptions.VerificationFailedException(url);

            return RemotingManager.CreateRemoteChannel<ICometRemoting>(url);
        }

        public static IRemoteChannel<ICometRemoting> CreateRemoting(string url, string token)
        {
            if (!Verification.Verifies(url, token).IsNotNullOrEmpty().Result())
                throw Exceptions.VerificationFailedException(url, token);

            return RemotingManager.CreateRemoteChannel<ICometRemoting>(url, token);
        }

        public static IRemoteChannel<ICometRemoting> CreateDynamicRemoting(string url)
        {
            if (!Verification.Verifies(url).IsNotNullOrEmpty().Result())
                throw Exceptions.VerificationFailedException(url);

            return RemotingManager.CreateDynamicRemoteChannel<ICometRemoting>(url);
        }

        public static IRemoteChannel<ICometRemoting> CreateDynamicRemoting(string url, string token)
        {
            if (!Verification.Verifies(url, token).IsNotNullOrEmpty().Result())
                throw Exceptions.VerificationFailedException(url, token);

            return RemotingManager.CreateDynamicRemoteChannel<ICometRemoting>(url, token);
        }

        public static ICometRemoting CreateWebRemoting()
        {
            return new CometRemoting();
        }


        /// <summary>
        /// 登录获取加密令牌
        /// </summary>
        /// <param name="senderCode"></param>
        /// <returns></returns>
        public static string Login(string senderCode)
        {
            if (string.IsNullOrEmpty(senderCode))
                throw Exceptions.VerificationFailedException(senderCode);

            return Pixysoft.Security.MD5.GetMD5(senderCode + StringHelper.global_framework_id);
        }
    }
}
